package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Oilwlhistory;
import com.example.onlinemonitoring.model.Oilwlsensor;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.chartres;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.OilwlsensorServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * 
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "外购油色谱", tags = { "|外购油色谱|设备接口" })
@RestController
@RequestMapping("/OilChromatogram")
public class OilwlsensorController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    OilwlsensorServiceImpl oilwlsensorServiceImpl;

    @ApiOperation(value = "修改设备参数", notes = "修改设备参数")
    @ResponseBody
    @RequestMapping(value = "/updateAll", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAll(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Oilwlsensor record = object.toJavaObject(Oilwlsensor.class);
        MyResult<Integer> m = new MyResult<Integer>();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        if (oilwlsensorServiceImpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "修改设备报警", notes = "修改设备报警")
    @ResponseBody
    @RequestMapping(value = "/updateAlarm", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateAlarm(@RequestBody String jsonParam,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Oilwlsensor record = new Oilwlsensor();
        record.setStationid(stationid);
        record.setDevicegroup(devicegroup);
        record.setDeviceaddr(deviceaddr);
        record.setC2h2alarmrelievevalue(object.getFloat("c2h2alarmrelievevalue"));
        record.setC2h2alarmvalue(object.getFloat("c2h2alarmvalue"));
        record.setC2h2warnrelievevalue(object.getFloat("c2h2warnrelievevalue"));
        record.setC2h2warnvalue(object.getFloat("c2h2warnvalue"));
        record.setC2h4alarmrelievevalue(object.getFloat("c2h4alarmrelievevalue"));
        record.setC2h4alarmvalue(object.getFloat("c2h4alarmvalue"));
        record.setC2h4warnrelievevalue(object.getFloat("c2h4warnrelievevalue"));
        record.setC2h4warnvalue(object.getFloat("c2h4warnvalue"));
        record.setC2h6alarmrelievevalue(object.getFloat("c2h6alarmrelievevalue"));
        record.setC2h6alarmvalue(object.getFloat("c2h6alarmvalue"));
        record.setC2h6warnrelievevalue(object.getFloat("c2h6warnrelievevalue"));
        record.setC2h6warnvalue(object.getFloat("c2h6warnvalue"));
        record.setCh4alarmrelievevalue(object.getFloat("ch4alarmrelievevalue"));
        record.setCh4alarmvalue(object.getFloat("ch4alarmvalue"));
        record.setCh4warnrelievevalue(object.getFloat("ch4warnrelievevalue"));
        record.setCh4warnvalue(object.getFloat("ch4warnvalue"));
        record.setCo2alarmrelievevalue(object.getFloat("co2alarmrelievevalue"));
        record.setCo2alarmvalue(object.getFloat("co2alarmvalue"));
        record.setCo2warnrelievevalue(object.getFloat("co2warnrelievevalue"));
        record.setCo2warnvalue(object.getFloat("co2warnvalue"));
        record.setCoalarmrelievevalue(object.getFloat("coalarmrelievevalue"));
        record.setCoalarmvalue(object.getFloat("coalarmvalue"));
        record.setCowarnrelievevalue(object.getFloat("cowarnrelievevalue"));
        record.setCowarnvalue(object.getFloat("cowarnvalue"));
        record.setGascontentalarmrelievevalue(object.getFloat("gascontentalarmrelievevalue"));
        record.setGascontentalarmvalue(object.getFloat("gascontentalarmvalue"));
        record.setGascontentwarnrelievevalue(object.getFloat("gascontentwarnrelievevalue"));
        record.setGascontentwarnvalue(object.getFloat("gascontentwarnvalue"));
        record.setH2alarmrelievevalue(object.getFloat("h2alarmrelievevalue"));
        record.setH2alarmvalue(object.getFloat("h2alarmvalue"));
        record.setH2warnrelievevalue(object.getFloat("h2warnrelievevalue"));
        record.setH2warnvalue(object.getFloat("h2warnvalue"));
        record.setN2alarmrelievevalue(object.getFloat("n2alarmrelievevalue"));
        record.setN2alarmvalue(object.getFloat("n2alarmvalue"));
        record.setN2warnrelievevalue(object.getFloat("n2warnrelievevalue"));
        record.setN2warnvalue(object.getFloat("n2warnvalue"));
        record.setO2alarmrelievevalue(object.getFloat("o2alarmrelievevalue"));
        record.setO2alarmvalue(object.getFloat("o2alarmvalue"));
        record.setO2warnrelievevalue(object.getFloat("o2warnrelievevalue"));
        record.setO2warnvalue(object.getFloat("o2warnvalue"));
        record.setPpmalarmrelievevalue(object.getFloat("ppmalarmrelievevalue"));
        record.setPpmalarmvalue(object.getFloat("ppmalarmvalue"));
        record.setPpmwarnrelievevalue(object.getFloat("ppmwarnrelievevalue"));
        record.setPpmwarnvalue(object.getFloat("ppmwarnvalue"));
        record.setPressurealarmrelievevalue(object.getFloat("pressurealarmrelievevalue"));
        record.setPressurealarmvalue(object.getFloat("pressurealarmvalue"));
        record.setPressurewarnrelievevalue(object.getFloat("pressurewarnrelievevalue"));
        record.setPressurewarnvalue(object.getFloat("pressurewarnvalue"));
        record.setTemperaturealarmrelievevalue(object.getFloat("temperaturealarmrelievevalue"));
        record.setTemperaturealarmvalue(object.getFloat("temperaturealarmvalue"));
        record.setTemperaturewarnrelievevalue(object.getFloat("temperaturewarnrelievevalue"));
        record.setTemperaturewarnvalue(object.getFloat("temperaturewarnvalue"));
        record.setTothydalarmrelievevalue(object.getFloat("tothydalarmrelievevalue"));
        record.setTothydalarmvalue(object.getFloat("tothydalarmvalue"));
        record.setTothydwarnrelievevalue(object.getFloat("tothydwarnrelievevalue"));
        record.setTothydwarnvalue(object.getFloat("tothydwarnvalue"));
        if (oilwlsensorServiceImpl.Update(record, false) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询实时表数据", notes = "查询实时表数据")
    @ResponseBody
    @GetMapping("/selectSensorData")
    public MyResult<Oilwlsensor> selectSensorData(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup) {
        Oilwlsensor record = new Oilwlsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        QueryObject qo = new QueryObject(page, limit);
        return oilwlsensorServiceImpl.getList(record, qo);
    }

    @ApiOperation(value = "修改监测点名称", notes = "修改监测点名称")
    @ResponseBody
    @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
            @RequestParam(value = "id", required = false) Long id) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        MyResult<Integer> m = new MyResult<Integer>();
        Oilwlsensor record = new Oilwlsensor();
        record.setId(id);
        record.setPointname(object.getString("pointname"));
        if (oilwlsensorServiceImpl.Update(record, true) >= 1) {
            m.setMsg("修改成功");
            m.setCode(0);
        } else {
            m.setMsg("修改失败");
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "查询历史表数据", notes = "查询历史表数据")
    @ResponseBody
    @GetMapping("/selectHistoryData")
    public MyResult<Oilwlhistory> selectHistoryData(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount) {
        Oilwlhistory record = new Oilwlhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        return oilwlsensorServiceImpl.getHisList(record, pagebase);
    }

    @ApiOperation(value = "查询历史表曲线", notes = "查询历史表曲线")
    @ResponseBody
    @GetMapping("/selectHistoryCurve")
    public MyResult<chartres> selectHistoryCurve(
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "lstr", required = false) String lstr) {
        Oilwlhistory record = new Oilwlhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        pagebase.setPage(page);
        pagebase.setLimit(limit);
        pagebase.setPagecount(pagecount);
        pagebase.setIsasc(true);
        return oilwlsensorServiceImpl.getHisCurveList(record, lstr, pagebase);
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToExcel")
    public void exportToExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = true) Integer stationid,
            @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
            @RequestParam(value = "start", required = false) String begintime,
            @RequestParam(value = "end", required = false) String endtime) throws IOException {
        Oilwlhistory record = new Oilwlhistory();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        pagebase pagebase = new pagebase();
        pagebase.setBegintime(begintime);
        pagebase.setEndtime(endtime);
        Workbook wb = oilwlsensorServiceImpl.writeToExcelByList(record, pagebase);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToSensorExcel")
    public void exportToSensorExcel(HttpServletResponse response,
            @RequestParam(value = "stationid", required = false) Integer stationid,
            @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
            @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr) throws IOException {
        Oilwlsensor record = new Oilwlsensor();
        record.setStationid(stationid);
        record.setDeviceaddr(deviceaddr);
        record.setDevicegroup(devicegroup);
        Workbook wb = oilwlsensorServiceImpl.writeToExcelBySensorList(record);
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
}
